package com.mdc.mansion.web.modules.system.web;

import java.util.Map;

import com.mdc.mansion.common.util.StringUtils;
import com.mdc.mansion.web.modules.system.utils.UploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.google.common.collect.Maps;
import com.mdc.mansion.web.common.web.BaseController;
import com.mdc.mansion.web.common.web.ResponseEntity;

@Controller
@RequestMapping(value = "${adminPath}/resource")
public class ResourceController extends BaseController {
	
	/**
	 * 
	 * @param request 文件封装
	 * @param name
	 * @param prePath
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping("upload")
	public ResponseEntity uploadFile(MultipartHttpServletRequest request,String name,String prePath){
		ResponseEntity responseEntity=null;
		Map<String, String> data=Maps.newHashMap();
		if(StringUtils.isEmpty(name) || request.getFile(name).isEmpty()){
			data.put("desc", "没有找到文件");
			responseEntity=ResponseEntity.createErrorResponseEntity(data, "上传文件出错");
		}else{
			MultipartFile uploadFile = request.getFile(name);
			try {
				String[] uploadFile2 = UploadUtils.uploadFile(uploadFile, name, prePath);
				data.put("filePath", uploadFile2[1]);
				data.put("fileUrl", uploadFile2[2]);
				responseEntity=ResponseEntity.createSuccessResponseEntity(data);
			} catch (Exception e) {
				responseEntity=ResponseEntity.createErrorResponseEntity(data, "上传文件出错");
				e.printStackTrace();
				this.loggerError(e, "上传文件出错");
			}
		}
		
		return responseEntity;
	}
}
